package com.sand.service.Imp;

import com.alibaba.fastjson.JSON;
import com.sand.dao.IBaseDao;
import com.sand.entity.InformationEntity;
import com.sand.entity.TRoleEntity;
import com.sand.entity.TUserEntity;
import com.sand.service.IInformationService;
import com.sand.util.PageModel;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

@Service(value = "informationService")
public class InformationServiceImp implements IInformationService {
/**
 *资讯控制层
 *
 * @author <achao1441470436@gmail.com>
 * @since 2021/12/1/001 20:17
 */
    //Dao层接口
    @Resource(name = "baseDao")
    private IBaseDao baseDao;

    //根据id查询资讯
    @Override
    public InformationEntity queryInformationById(int rid,int showWi) {
        List<InformationEntity> list = baseDao.queryObject("from InformationEntity t where t.rid=?0", rid);
        InformationEntity infor = list.get(0);
        if(showWi>0){
            infor.setReadNumber(infor.getReadNumber()+1);
            baseDao.updateObject(infor);
            return infor;
        }
        return infor;
    }

    //分页查询资讯
    @Override
    public PageModel<InformationEntity> queryInformationLimit(int limit, int page, String condition) {
        PageModel<InformationEntity> pm = new PageModel<InformationEntity>();
        //计算开始位置
        int begin=(page-1)*limit;
        //创建查询对象
        DetachedCriteria dc = DetachedCriteria.forClass(InformationEntity.class);
        //添加模糊查询条件
        if(condition!=null){
            dc.add(Restrictions.like("title","%"+condition+"%"));
        }

        List<Long> list1 = baseDao.queryObjectCommon(dc.setProjection(Projections.rowCount()));
        dc.setProjection(null);
        dc.addOrder(Order.desc("rid"));
        List<InformationEntity> list = baseDao.queryObjectLimit(dc, begin, limit);
        long i=list1.get(0);
        int count= (int) i;

        pm.setList(list);
        pm.setPageNo(page);
        pm.setPageSize(limit);
        pm.setPagecount(count);
        return pm;
    }

    //添加资讯
    @Override
    public boolean addInformation(String data) {
        //创建时间
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());

        if(data!=null){
            //解析json数据
            InformationEntity infor = JSON.parseObject(data, InformationEntity.class);
            infor.setCreateTime(timestamp);
            boolean bln = baseDao.addObject(infor);
            if(bln){
                return true;
            }
        }
        return false;
    }

    //删除资讯
    @Override
    public boolean delInformation(String data) {
        if(data!=null){
            //解析json数据
            List<InformationEntity> list = JSON.parseArray(data, InformationEntity.class);
            boolean bln = baseDao.delAllObject(list);
            if(bln){
                return true;
            }
        }
        return false;
    }

    //修改资讯
    @Override
    public boolean updateInformation(String data) {
        if(data!=null){
            //解析json数据
            InformationEntity infor = JSON.parseObject(data, InformationEntity.class);
            List<InformationEntity> list = baseDao.queryObject("from InformationEntity t where t.rid=?0", infor.getRid());
            infor.setReadNumber(list.get(0).getReadNumber());
            boolean bln = baseDao.updateObject(infor);
            if(bln){
                return true;
            }
        }
        return false;
    }
}
